﻿namespace HostTracker.Azure.Management

open System
open System.ServiceModel
open System.ServiceModel.Web
open System.Runtime.Serialization
open HostTracker.Azure.Management.FSharpExtensions
(*
<Subscription xmlns=http://schemas.microsoft.com/windowsazure   xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <SubscriptionID>subscription-id</SubscriptionID>
    <SubscriptionName>name-of-the-subscription</SubscriptionName>
    <SubscriptionStatus>Active</SubscriptionStatus>
    <AccountAdminLiveEmailId>account-admin-live-id</AccountAdminLiveEmailId>
    <ServiceAdminLiveEmailId>service-admin-live-id</ServiceAdminLiveEmailId>
    <MaxCoreCount>maximum-cores-allocable</MaxCoreCount>
    <MaxStorageAccounts>maximum-storage-accounts-allocable</MaxStorageAccounts>
    <MaxHostedServices>maximum-hosted-services-allocable</MaxHostedServices>
    <CurrentCoreCount>currently-allocated-cores</CurrentCoreCount>
    <CurrentHostedServices>currently-allocated-hosted-services</CurrentHostedServices>
    <CurrentStorageAccounts>currently-allocated-storage-accounts</CurrentStorageAccounts> 
</Subscription>
*)

type SubscriptionStatus = Active | Disabled
with
    override x.ToString() = Reflection.unionToStringPlain x 
    static member Parse(str) = Reflection.stringToUnionPlain<SubscriptionStatus> str

[<DataContractAttribute(Name="Subscription", Namespace="http://schemas.microsoft.com/windowsazure")>]
type Subscribtion() =
    let mutable id = Guid.Empty
    let mutable name = ""
    let mutable status = Active
    let mutable accountAdminLiveId = ""
    let mutable serviceAdminLiveId = ""
    let mutable cores = 0
    let mutable storages = 0
    let mutable hostedServices = 0
    let mutable curCores = 0
    let mutable curHostedServices = 0
    let mutable curStorages = 0
    [<DataMember(Name="SubscriptionID", Order = 1)>]
    member x.Id with get () = id and set v = id <- v
    [<DataMember(Name="SubscriptionName", Order = 2)>]
    member x.Name with get () = name and set v = name <- v
    [<DataMember(Name="SubscriptionStatus", Order = 3)>]
    member x.StatusStr with get () = status.ToString() and set v = status <- SubscriptionStatus.Parse v
    member x.Status = status
    [<DataMember(Name="AccountAdminLiveEmailId", Order = 4)>]
    member x.AccountAdminLiveId with get () = accountAdminLiveId and set v = accountAdminLiveId <- v
    [<DataMember(Name="ServiceAdminLiveEmailId", Order = 5)>]
    member x.ServiceAdminLiveId with get () = serviceAdminLiveId and set v = serviceAdminLiveId <- v
    [<DataMember(Name="MaxCoreCount", Order = 6)>]
    member x.MaxCoreCount with get () = cores and set v = cores <- v
    [<DataMember(Name="MaxStorageAccounts", Order = 7)>]
    member x.MaxStorageAccounts with get () = storages and set v = storages <- v
    [<DataMember(Name="MaxHostedServices", Order = 8)>]
    member x.MaxHostedServices with get () = hostedServices and set v = hostedServices <- v
    [<DataMember(Name="CurrentCoreCount", Order = 9)>]
    member x.CurrentCoreCount with get () = curCores and set v = curCores <- v
    [<DataMember(Name="CurrentHostedServices", Order = 10)>]
    member x.CurrentHostedServices with get () = curHostedServices and set v = curHostedServices <- v
    [<DataMember(Name="CurrentStorageAccounts", Order = 11)>]
    member x.CurrentStorageAccounts with get () = curStorages and set v = curStorages <- v

[<Interface>]
[<ServiceContract>]
type ISubscribtionManagement = 
    [<OperationContract>]
    [<WebInvoke(UriTemplate="/{id}", Method="GET")>]
    abstract GetSubscribtion: id:string -> Subscribtion
    [<OperationContract(AsyncPattern=true)>]
    abstract BeginGetSubscribtion: id:string*callback:AsyncCallback*state:obj -> IAsyncResult
    abstract EndGetSubscribtion: result: IAsyncResult -> Subscribtion
    (*
    TODO: this is operation is not easy
    [<OperationContract>]
    [<WebInvoke(UriTemplate="/operations?StartTime={startTime}&EndTime={endTime}&ObjectIdFilter={objUrl}&OperationResultFilter={resultFilter}&ContinuationToken={contToken}", Method="GET")>]
    abstract ListOperations: unit -> Subscribtion
    *)